/**
 * 
 */
package com.android.weibogo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * @author xiang.jianhua
 *
 */
public class WeiboWrite extends Activity {

	private String mTokenKey;
	private String mTokenSecret;
	private String mUserName;
	private String mWeiboName;
	private TecentWeiboModel mWeiboModel;
	private boolean mHasPic;
	
	private static final String mTag = "WeiboWrite";
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo_write);
		LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);
		layout.setBackgroundResource(R.drawable.background);
		
		Intent intent = getIntent();
		mTokenKey = intent.getStringExtra("tokenkey");
		mTokenSecret = intent.getStringExtra("tokensecret");
		mUserName = intent.getStringExtra("username");
		mWeiboName = intent.getStringExtra("weiboname");
		
		
		mWeiboModel = new TecentWeiboModel(this, mTokenKey, mTokenSecret, null);
		
		if (mTokenKey != null && mTokenSecret != null) {
			Button actionBtn = (Button)findViewById(R.id.action_button);
			actionBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String pic = null;
					EditText et = (EditText)findViewById(R.id.weibo_edit_text);
					if(et.getText().toString().length() <= 0){
						Builder builder = new AlertDialog.Builder(WeiboWrite.this);
						builder.setCancelable(true);
						builder.setTitle(getString(R.string.system_help_info));
						builder.setMessage(getString(R.string.content_null));
						builder.create().show();
					}else{
						if (mWeiboModel.newWeibo(et.getText().toString(), pic)) {
							Toast.makeText(getApplicationContext(),
									getString(R.string.write_weibo_success),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(getApplicationContext(),
									getString(R.string.write_weibo_fail),
									Toast.LENGTH_LONG).show();
						}
					}
				}
			});
			//set the click listener for the picture selection button
			ImageButton picSelBtn = (ImageButton)findViewById(R.id.write_pic_imgbtn);
			picSelBtn.setOnTouchListener(new ImageButtonTouchListener());
			picSelBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					 Intent localIntent = new Intent();
                     localIntent.setType("image/*");
                     localIntent.setAction("android.intent.action.GET_CONTENT");
                     Intent localIntent2 = Intent.createChooser(localIntent, "Select Picture");
                     startActivityForResult(localIntent2, 0);
				}
			});
		}else {
			Log.v(mTag, "invalid parameters, unkown error");
			finish();
		}
	}

}
